package com.abaaba.project2.service.serviceImpl;

import com.abaaba.project2.bean.Footprint;
import com.abaaba.project2.bean.FootprintExample;
import com.abaaba.project2.bean.vo.AdminFootprintVo;
import com.abaaba.project2.mapper.FootprintMapper;
import com.abaaba.project2.service.AdminFootprintService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminFootprintServiceImpl implements AdminFootprintService {

    @Autowired
    FootprintMapper footprintMapper;

    @Override
    public AdminFootprintVo getFootprintList(Integer page, Integer limit, Integer userId, Integer goodsId, String sort, String order) {
        PageHelper.startPage(page, limit);
        FootprintExample footprintExample = new FootprintExample();
        footprintExample.setOrderByClause(sort + " " + order);
        FootprintExample.Criteria criteria = footprintExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (goodsId != null) {
            criteria.andGoodsIdEqualTo(goodsId);
        }

        List<Footprint> footprints = footprintMapper.selectByExample(footprintExample);
        PageInfo<Footprint> footprintPageInfo = new PageInfo<>(footprints);
        long total = footprintPageInfo.getTotal();

        AdminFootprintVo adminFootprintVo = new AdminFootprintVo();
        adminFootprintVo.setItems(footprints);
        adminFootprintVo.setTotal(total);
        return adminFootprintVo;
    }
}
